import { Texture } from "@pixi/core";
import { Loader } from "@pixi/loaders";

type Res = { [key: string]: { texture: Texture } };

export class LoaderManager {
  public static loader: Loader | undefined = undefined;
  public static resource: Res = {};
  public static onLoader(urls: { name: string; url: string }[]) {
    return new Promise((res: Function) => {
      // 筛选出已经加载过的
      const urlArr = urls.filter((u) => {
        return !LoaderManager.resource[u.name];
      });

      LoaderManager.loader
        ?.add(urlArr)
        .load((loader: Loader, resource: Res) => {
          // 加载完成则合并资源
          LoaderManager.resource = { ...LoaderManager.resource, ...resource };
          res();
        });
    });
  }
}
